﻿@using OBUCare.Web.Infrastructure;
@using OBUCare.Core.Models;
@using OBUCare.Core.BL
@using OBUCare.Core.DataAccess;
@{  
    ObuDropDownListHlper ddlHelper = new ObuDropDownListHlper();
    //this.Layout = "~/Views/Shared/_MinLayout.cshtml";
}
<h2 style="text-align: center; height: 100px;">
    元器件入库登记</h2>
@using (Html.BeginForm("Incoming", "Parts", FormMethod.Post, new { @id = "form_id" }))
{ 

    <div>
        <table style="width: 100%" id="tblParts">
            <tbody>
                <tr>
                    <td>
                        元器件：
                    </td>
                    <td>
                        @Html.Telerik().DropDownList().Name("PartType").HtmlAttributes(new { @style = "width:120px;" }).BindTo(ddlHelper.GetPartTypes(null, true)).ClientEvents(o => o.OnChange("changeRebind"))
                    </td>
                    <td colspan="6">
                        <input type="button" value="增行" onclick="loadPartControl();" />
                    </td>
                    <td style="width: 100px;">
                        &nbsp;
                    </td>
                </tr>
                @Html.Partial("PartControl", 0)
            </tbody>
        </table>
    </div>

    <div>
        <table style="width: 100%">
            <tr style="height: 100px;">
                <td style="width: 70%;">
                    <input type="hidden" id="hidPartControlIndex" name="hidPartControlIndex" />
                </td>
                <td align="center">
                    <input type="button" value="入库" onclick="submitForm(false);" />
                    @Html.Hidden("specAll")
                </td>
                <td align="center">
                    <input type="button" value="入库并打印" onclick="submitForm(true);" />
                </td>
            </tr>
        </table>
    </div>
  
    
@*<div>
        @(Html.Telerik().Window()
        .Name("Window")       
        .Buttons(buttons => buttons.Maximize().Close())
        .Width(900)
        .Draggable(true)
        .Title("元器件入库")
        .Visible(false)
        .ClientEvents(events => events.OnClose("Window_onClose"))        
        .Content
            (
                @<text>
                    @Html.Partial("StockInPrint")
                </text>
            )
        )
    </div>*@
        
}
<div id="divtempContainer">
</div>
<script language="javascript" type="text/javascript">
    var PartControlIndex = 1;
    var PartControlCount = 1;
    function loadPartControl() {

        var d = new Date();
        var dateTimeNow = d.getTime();

        var url = '/Parts/PartControl/' + PartControlIndex++;
        url = url + "?dt=" + dateTimeNow;
        $('#divtempContainer').load(url, function (response, status) {

            //            alert(status);
            //            alert(response);
            if (status == 'success') {
                $("#tblParts").append(response);
                $('#divtempContainer').html("");
                PartControlCount++;
                //getAccessoriesValues();  
            }
        });
    }

    function delTr(k) {
        //debugger;
        if (PartControlCount == 1)
            alert('不允许删除最后一行');
        else {
            $(k).parent().parent().remove();
            PartControlCount--;
        }
    }

    function submitForm(isPrint) {

        //debugger;
        $("#hidPartControlIndex").val(PartControlIndex);
        var form = $("#form_id");
        var specID = "";
        for (var i = 0; i < PartControlIndex; i++) {
            if ($('#Supplier' + i).val() == "" || $('#Supplier' + i).val() == "请选择") {
                alert("请选择厂商");
                return false;
            }
            if ($('#SpecName' + i).val() == "" || $('#SpecName' + i).val() == "请选择" || $('#SpecName' + i).val() == "0") {
                alert("请选择型号");
                return false;
            }
            if ($('#RecordDate' + i).val() == "") {
                alert("请选择入库日期");
                return false;
            }
            if ($('#Amount' + i).val() == "0") {
                alert("请输入入库数量");
                return false;
            }
            specID += $('#SpecName' + i).val() + ",";

        }
        $('#specAll').val(specID);
        dataString = form.serialize();
        $.ajax({
            type: "POST",
            url: form.attr('action'),
            data: dataString,
            cache: false,
            dataType: "json",
            success: function (data) {
                if (data.success) {
                    alert("入库成功");
                    //$("#form_id")[0].reset();
                    if (!isPrint)
                        window.location.href = "/Parts/Index/";
                    else {
                        OpenPrintWin(data.ids);
                        window.location.href = "/Parts/Index/";
                    }
                }
                else {
                    alert('提交失败：' + data.ErrorMsg)
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var err = XMLHttpRequest + "<br />" + textStatus + "<br />" + errorThrown;
                alert(err);
            }
        });

    }
    function changeSpecs(e) {

        var index = e.currentTarget.name.substring(8);
        var partsType = $("#PartType").data("tDropDownList").value();
        var SpecName = $('#SpecName' + index).data('tDropDownList');
        var value = e.currentTarget.value;
        $.post("/Parts/_getSpecs", { "supplierID": value, "PartsType": partsType }, function (data) {
            SpecName.dataBind(data);
            if (SpecName != null) {
                SpecName.select(0);
            }
        });
    }
    function changeRebind(e) {
        var partsType = e.currentTarget.value;
        for (var i = 0; i < PartControlCount; i++) {
            var Supplier = $('#Supplier' + i).data('tDropDownList');
            Supplier.reload();
            var SpecName = $('#SpecName' + i).data('tDropDownList');
            SpecName.reload();
        }
    }

    //打开打印页
    function OpenPrintWin(ids) {
        /*$("#hidIDs").val(ids);

        var grid = $('#Grid').data('tGrid')        
        grid.rebind();

        var partType = $("#PartType").data("tDropDownList").text();
        $('#lblPartType').text(partType);
        var window = $("#Window").data("tWindow");
        window.center();
        window.open();*/

        window.open("/parts/StockInPrint/" + ids);
    }

    //关闭窗口时，跳转到初始页面
    function Window_onClose() {
        window.location.href = "/Parts/Index/";
    }
</script>
@{
    ddlHelper.Dispose();    
}
